<?php
/**
 * Created by PhpStorm.
 * User: *!N.J
 * Date: 2021/9/6
 * Time: 10:42
 * QQ:1467572213
 */

namespace app\agent\model;

use think\Model;
use think\Db;
use think\facade\Config;

class Api extends Model
{
    public $user_balance = 1;
    public $aa = 2;

    public static function get_auto_grab()
    {
        $auto_grab = Db::name('fd_user')->where('auto_grab',1)->select();
        return $auto_grab;
    }

    public static function get_group_info($id)
    {
        $group_info = Db::name('fd_group')->where('id',$id)->find();
        return $group_info;
    }

    public static function auto_push_order($user_id)
    {
        $user_info = Base::get_user_info($user_id);

        $where['group_id'] = $user_info['group'];
        $where['odd_num'] = $user_info['group_order'] + 1;
        $group_info = Db::name('fd_group_mode')->where($where)->find();
        if($group_info['pay_value'] == 0){
            $yjbl = Db::name('fd_level')->where('id',$user_info['level'])->value('yjbl');
            $group_info['pay_value'] = $yjbl / 100;
        }
        // var_dump($group_info);die;
        if ($group_info){
            switch ($group_info['grab_type'])
            {
                case 1:
//                    $money = Db::name('fd_level')->where('id',$user_info['level'])->value('money');
                    $money = $user_info['balance'];
                    $goods_info = self::get_goods($money);
                    $push_goods['order_id'] = 'A'.date('md').time().mt_rand(111,999);
                    $push_goods['aid'] = $user_info['pid'];
                    $push_goods['uid'] = $user_info['id'];
                    $push_goods['username'] = $user_info['username'];
                    $push_goods['phone'] = $user_info['phone'];
                    $push_goods['grab_type'] = $group_info['grab_type'];
                    $push_goods['goods_id'] = $goods_info['id'];
                    $push_goods['goods_name'] = $goods_info['goods_name'];
                    $suiji = mt_rand(95,100);
                    $suiji = $suiji / 100;
                    $goods_price = $user_info['balance'] * $suiji;
                    switch ($group_info['pay_mode'])
                    {
                        case 1:
                            $push_goods['goods_price'] = $goods_price;
                            $push_goods['order_commission'] = $group_info['pay_value'];
                            $push_goods['order_earnings'] = $goods_price * $group_info['pay_value'];
                            break;
                        case 2:
                            $push_goods['goods_price'] = $goods_price;
                            $push_goods['order_commission'] = 0;
                            $push_goods['order_earnings'] = $group_info['pay_value'];
                            break;
                    }
                    $push_goods['goods_pic'] = $goods_info['goods_pic'];
                    $push_goods['goods_type'] = $goods_info['type'];
                    $push_goods['sub_time'] = time() + 1 * 3600;
                    $push_goods['s_num'] = 1;
                    $push_goods['e_num'] = 1;
                    if ($group_info['is_windows'] == 1 && $push_goods['e_num'] == 1){
                        $push_goods['is_pop'] = 1;
                        $push_goods['pop_id'] = $group_info['windows_img'];
                    }else{
                        $push_goods['is_pop'] = 0;
                        $push_goods['pop_id'] = 0;
                    }
                    $push_goods['status'] = 0;
                    $push_goods['create_time'] = time();
                    $add_push = Db::name('fd_order')->insert($push_goods);
                    if ($add_push == true){
                        $stop_grab = Db::name('fd_user')->where('id',$user_id)->setField('auto_grab',0);
                        if ($stop_grab == true){
                            return 'success';
                        }else{
                            return 'error_stop';
                        }
                    }else{
                        return 'error';
                    }
                    break;
                case 2:
                    return false;
                    break;
                case 3:
                    $order_num =  explode(',',$group_info['addition']);
                    // var_dump($user_info['balance']);
                    // var_dump($order_num);die;
                    $num = count($order_num);
                    $i = 1;
                    foreach ($order_num as $k => $v){
                        if($i > $num) break;
                        self::batch_order($user_info,$v,$group_info,$i,$num);
                        $i++;
                    }
                    Db::name('fd_user')->where('id',$user_id)->setField('auto_grab',0);
                    break;
            }
        }
    }

    private static function batch_order($user_info,$bili,$group_info,$i,$num)
    {
        $money = $user_info['balance'] * $bili;
        $goods_info = self::get_goods($money);
        $push_goods['order_id'] = 'A'.date('md').time().mt_rand(111,999);
        $push_goods['aid'] = $user_info['pid'];
        $push_goods['uid'] = $user_info['id'];
        $push_goods['username'] = $user_info['username'];
        $push_goods['phone'] = $user_info['phone'];
        $push_goods['grab_type'] = $group_info['grab_type'];
        $push_goods['goods_id'] = $goods_info['id'];
        $push_goods['goods_name'] = $goods_info['goods_name'];
        // $goods_price = $money - mt_rand(1,15);
        $goods_price = $money;
        switch ($group_info['pay_mode'])
        {
            case 1:
                $push_goods['goods_price'] = $goods_price;
                $push_goods['order_commission'] = $group_info['pay_value'];
                $push_goods['order_earnings'] = $goods_price * $group_info['pay_value'];
                break;
            case 2:
                $push_goods['goods_price'] = $goods_price;
                $push_goods['order_commission'] = 0;
                $push_goods['order_earnings'] = $group_info['pay_value'];
                break;
        }
        $push_goods['goods_pic'] = $goods_info['goods_pic'];
        $push_goods['goods_type'] = $goods_info['type'];
        $push_goods['sub_time'] = time() + 1 * 3600;
        $push_goods['s_num'] = $i;
        $push_goods['e_num'] = $num;
        if ($group_info['is_windows'] == 1){
            $push_goods['is_pop'] = 1;
            $push_goods['pop_id'] = $group_info['windows_img'];
        }else{
            $push_goods['is_pop'] = 0;
            $push_goods['pop_id'] = 0;
        }
        $push_goods['status'] = 0;
        $push_goods['create_time'] = time();
        Db::name('fd_order')->insert($push_goods);
    }

    public static function goods_info($id)
    {
        $goods_info = Db::name('fd_goods_list')->where('id',$id)->find();
        return $goods_info;
    }

    public static function user_balanceaa($money,$price)
    {
        static $money;
        $a = $money - $price;
        return $a;
    }

    public static function get_goods($balance)
    {
        $range_min = round($balance - $balance * 0.5,2);
        $range_max = round($balance + $balance * 0.5,2);
        $where = [
            ['goods_price', '>=', $range_min],
            ['goods_price', '<=', $range_max],
        ];
        $goods = Db::name('fd_goods_list')
            ->where($where)
            ->orderRaw('rand()')
            ->find();
        if (!$goods){
            $goods = self::get_max_goods();
        }
        return $goods;
    }

    public static function get_max_goods()
    {
        $goods = Db::name('fd_goods_list')
            ->order('goods_price','desc')
            ->limit(20)->select();
        return $goods[mt_rand(0,count($goods) - 1)];
    }

    public static function rand_goods()
    {
        $min = Db::name('fd_goods_list')->min('id');
        $max = Db::name('fd_goods_list')->max('id');
        $rand_goods = Db::name('fd_goods_list')->where('id',mt_rand($min,$max))->find();
        if ($rand_goods){
            return $rand_goods;
        }else{
            self::rand_goods();
        }
    }

    public static function frozen_order()
    {
        $now_time = time();
        $where = [
            ['status', '=', 0],
            ['sub_time', '<', $now_time],
        ];
        $order_info = Db::name('fd_order')->where($where)->select();
        if ($order_info){
            foreach ($order_info as $k => $v){
                Db::name('fd_order')->where('id',$v['id'])->setField('status',3);
            }
            return 'success';
        }else{
            return 'not_order';
        }
    }

    public static function settle_order()
    {
        $now_time = time();
        $where = [
            ['status', '=', 2],
            ['settle_time', '<', $now_time],
        ];
        $get_order = Db::name('fd_order')->where($where)->select();
        foreach ($get_order as $k => $v){
            $where = [
                ['uid', '=', $v['uid']],
                ['status', 'in', [0,3]],
            ];
            $check_order = Db::name('fd_order')->where($where)->count();
            if ($check_order == 0){
                self::settlement($v['id']);
                echo 'success';
            }else{
                echo 'not_order';
            }
        }
        die;
        $where = [
            ['uid', '=', $v['uid']],
            ['status', 'in', [0,3]],
        ];

        if ($get_order){
            $where = [
                ['uid', '=', $get_order['uid']],
                ['status', 'in', [0,3]],
            ];
            $check_order = Db::name('fd_order')->where($where)->count();
            if ($check_order == 0){
                $select = [
                    ['uid', '=', $get_order['uid']],
                    ['status', '=', 2],
                ];
                $order_select = Db::name('fd_order')->where($select)->select();
                if ($order_select){
                    foreach ($order_select as $k => $v){
                        $order_info = Db::name('fd_order')->where('id',$v['id'])->find();
                        $balance = $order_info['goods_price'] + $order_info['order_earnings'];
                        Db::name('fd_order')->where('id',$order_info['id'])->setField('status',1);
                        Db::name('fd_user')->where('id',$order_info['uid'])->setDec('freeze',$order_info['goods_price']);
                        Db::name('fd_user')->where('id',$order_info['uid'])->setInc('balance',$balance);
                        Db::name('fd_user')->where('id',$order_info['uid'])->setInc('total',$order_info['order_earnings']);
                    }
                    Db::name('fd_user')->where('id',$order_info['uid'])->setInc('finish_order',1);
                }else{
                    return 'not_time';
                }
                Db::name('fd_user')->where('id',$get_order['uid'])->setInc('group_order',1);
                return 'success';
            }else{
                return 'not_check';
            }
        }else{
            return 'not_order';
        }
    }

    private static function settlement($id)
    {
        $order_info = Db::name('fd_order')->where('id',$id)->find();
        $balance = $order_info['goods_price'] + $order_info['order_earnings'];
        Db::name('fd_order')->where('id',$order_info['id'])->setField('status',1);
        Db::name('fd_user')->where('id',$order_info['uid'])->setDec('freeze',$order_info['goods_price']);
        Db::name('fd_user')->where('id',$order_info['uid'])->setInc('balance',$balance);
        Db::name('fd_user')->where('id',$order_info['uid'])->setInc('total',$order_info['order_earnings']);
        $select = [
            ['uid', '=', $order_info['uid']],
            ['status', '=', 2],
        ];
        $is_over = Db::name('fd_order')->where($select)->count();
        if ($is_over == 0){
            Db::name('fd_user')->where('id',$order_info['uid'])->setInc('finish_order',1);
            Db::name('fd_user')->where('id',$order_info['uid'])->setInc('group_order',1);
            Db::name('fd_user')->where('id',$order_info['uid'])->setInc('day_o',1);
        }
    }

    public static function del_order_num()
    {
        $user = Db::name('fd_user')->where('status',1)->select();
        foreach ($user as $k => $v){
            Db::name('fd_user')->where('id',$v['id'])->setField('day_o',0);
        }
        return 'success';
    }

}